<?php

namespace App\Models;

use GuzzleHttp\Client;
use GuzzleHttp\Utils;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
/**
 * App\Models\OilPrice
 *
 * @property int $id
 * @property string $city 城市名称
 * @property string $ninety_two 92号
 * @property string $ninety_five 95号
 * @property string $ninety_eight 98号
 * @property string $zero 0号
 * @property \Illuminate\Support\Carbon $created_at 创建时间
 * @property \Illuminate\Support\Carbon $updated_at 更新时间
 * @property \Illuminate\Support\Carbon|null $deleted_at 删除时间
 * @method static \Illuminate\Database\Eloquent\Builder|OilPrice newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|OilPrice newQuery()
 * @method static \Illuminate\Database\Query\Builder|OilPrice onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|OilPrice query()
 * @method static \Illuminate\Database\Eloquent\Builder|OilPrice whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|OilPrice whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|OilPrice whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|OilPrice whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|OilPrice whereNinetyEight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|OilPrice whereNinetyFive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|OilPrice whereNinetyTwo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|OilPrice whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|OilPrice whereZero($value)
 * @method static \Illuminate\Database\Query\Builder|OilPrice withTrashed()
 * @method static \Illuminate\Database\Query\Builder|OilPrice withoutTrashed()
 * @mixin \Eloquent
 */
class OilPrice extends Model
{
    public $guzzleClient ;
    public $guzzleOptions;
    use HasFactory,SoftDeletes;
    //可以批量赋值
    protected $fillable = [
        'city','ninety_two','ninety_five',
        'ninety_eight','zero',
        'created_at','updated_at','deleted_at'
    ];

    /**
     * @param $configUrl
     * @return Client
     */
    public function setClient($configUrl)
    {
        $this->guzzleClient =  new Client(array('base_uri' => $configUrl));
        return $this->guzzleClient;
    }

    /**
     * 后台更新数据
     * @return array
     */
    public function adminUpdateOilPrice()
    {
        //获取今日油价
        $insertData = [];
        $this->setClient(config('third_party_api.oil_prices.url'));
        $oilPricesCurrent = Utils::jsonDecode($this->guzzleClient->request('get','',config('third_party_api.oil_prices.option'))->getBody()->getContents(),true);
        if(isset($oilPricesCurrent['error_code'])&&($oilPricesCurrent['error_code'] == 0)&&(!empty($oilPricesCurrent['result'])))
        {
            foreach ($oilPricesCurrent['result'] as $singleData)
            {
                $insertData[] = [
                    'city' => $singleData['city'],
                    'ninety_two' => $singleData['92h'],
                    'ninety_five' => $singleData['95h'],
                    'ninety_eight' => $singleData['98h'],
                    'zero' => $singleData['0h'],
                    'created_at' => strtotime(date('Y-m-d')),
                    'updated_at' => strtotime(date('Y-m-d H:i:s')),
                ];
            }
        }
        if(!empty($insertData))
        {
            DB::table('oil_prices')->upsert($insertData,['city','created_at']);
        }
        return $insertData;
    }
}
